package com.shopping.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.shopping.system.model.SysMenu;
import com.shopping.system.service.SysMenuService;
import com.shopping.common.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName SysMenuController
 * @Description 菜单管理
 * @Version 1.0
 * @create 2020/12/21 15:1
/**
 * 菜单管理 controller
 */
@RestController
@RequestMapping("/sysMenu")
@Api(tags = "菜单管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping
    @ApiOperation(value = "获取所有菜单信息", notes = "获取所有菜单信息")
    public Result getGoods(@ApiParam(name = "sysMenu", value = "菜单信息") SysMenu sysMenu) {
//        throw new RuntimeException("模拟异常");
        // 构建查询条件
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(sysMenu.getName())) {
            queryWrapper.like("name", sysMenu.getName());
        }
        // 分页
        PageHelper.startPage(sysMenu.getPage(), sysMenu.getSize());
        return Result.ok(sysMenuService.list(queryWrapper));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取菜单信息", notes = "根据id获取菜单信息")
    public Result getGoodsById(@ApiParam(name = "id", value = "菜单id", required = true) @PathVariable Integer id) {
        return Result.ok(sysMenuService.getById(id));
    }


    @PostMapping
    @ApiOperation(value = "根据id修改菜单信息", notes = "根据id修改菜单信息")
    public Result updateGoodsById(@ApiParam(name = "sysMenu", value = "菜单信息", required = true) SysMenu sysMenu) {
        return Result.ok(sysMenuService.updateById(sysMenu));
    }


    @PutMapping
    @ApiOperation(value = "新增菜单信息", notes = "新增菜单信息")
    public Result addGoods(@ApiParam(name = "sysMenu", value = "菜单信息", required = true) SysMenu sysMenu) {
        return Result.ok(sysMenuService.save(sysMenu));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除菜单信息", notes = "根据id删除菜单信息")
    public Result deleteGoodsById(@ApiParam(name = "id", value = "菜单id", required = true) @PathVariable Integer id) {
        return Result.ok(sysMenuService.removeById(id));
    }

}